package com.adtech.adms.commons.web.tag;

import java.util.List;
import java.util.Stack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.commons.util.TreeUtil;
import com.adtech.adms.commons.util.TreeUtil.OnGetNodeListener;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.User;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Dormin 
 * @since 2011-5-31 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class NavigationTag extends ComponentTagSupport{
	/**
	 * @function
	 * @param
	 *
	 * @return
	 * @exception
	 *
	 * @author
	 * @history
	 *
	 */
	private static final long serialVersionUID = 7824895611501328761L;
	/**
	 * 文字显示
	 */
	private String text;
	/**
	 * 样式
	 */
	private String style="color:#295280; font-weight:bold;x";

	/**
	 * 组建id 用于domument
	 */
	private String id;
	/**
	 * 该页面所对应的模块的code用于返回页面的显示值 （此项为必填项）
	 */
	private String moduleCode;
	/**
	 * 服务器地址
	 */
	private String requestPath;
	/**
	 * 连接的css
	 */
	private String linkStyle="color:#295280; font-weight:bold;x";

	/**
	 * 连接的Class
	 */
	private String linkClass;

	private String onClick;

	public String getOnClick() {
		return onClick;
	}


	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}


	public String getLinkClass() {
		return linkClass;
	}


	public void setLinkClass(String linkClass) {
		this.linkClass = linkClass;
	}


	public String getLinkStyle() {
		return linkStyle;
	}


	public void setLinkStyle(String linkStyle) {
		this.linkStyle = linkStyle;
	}


	public String getText() {
		return text;
	}


	public void setText(String text) {
		this.text = text;
	}


	public String getStyle() {
		return style;
	}


	public void setStyle(String style) {
		this.style = style;
	}


	public String getRequestPath() {
		return requestPath;
	}


	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}


	@Override
	public String getId() {
		return id;
	}


	@Override
	public void setId(String id) {
		this.id = id;
	}
	
	public String getModuleCode() {
		return moduleCode;
	}
	
	public void setModuleCode(String moduleCode) {
		this.moduleCode = moduleCode;
	}


	@Override
	public Component getBean(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		String path = request.getContextPath();
		String bathpath=request.getScheme()+ "://"+request.getServerName()+ ":"+request.getServerPort()+ path+"/";
		setRequestPath(bathpath);
		NavigationComponent component= new NavigationComponent(stack);
		User user = (User)request.getSession().getAttribute(SessionKey.SESSION_KEY_USER);
		List<TreeNode<Feature>> nodes=user.getFeaturesTree();
		Stack<Feature> features=new Stack<Feature>();
		TreeNode<Feature> node=null;
		for (TreeNode<Feature> treeNode : nodes) {
			node= TreeUtil.iteratorNodes(treeNode, new OnGetNodeListener<Feature>() {

				public boolean onGetNode(TreeNode<Feature> node) {
					if(moduleCode.equals(node.getInnerObject().getCode())){
						return true;
					}
					return false;
				}

			});
			if(node!=null){
				setNodePath(features, node.getInnerObject());
				break;
			}
		}

		component.setFeatures(features);
		return component;
	}

	private static void setNodePath(Stack<Feature> nodes,Feature node) {
		nodes.push(node);
		if(node.getParent()!=null){
			setNodePath(nodes,node.getParent());
		}
	}
	@Override
	protected void populateParams() {
		super.populateParams();
		NavigationComponent navigationComponent=(NavigationComponent)component;
		navigationComponent.setId(id);
		navigationComponent.setStyle(style);
		navigationComponent.setText(text);
		navigationComponent.setModuleCode(moduleCode);
		navigationComponent.setRequestPath(requestPath);
		navigationComponent.setLinkStyle(linkStyle);
		navigationComponent.setLinkClass(linkClass);
		navigationComponent.setOnClick(onClick);
	}
}
